package com.qidian.mes.pro.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.pojo.pro.ProWorkorder;
import com.qidian.mes.pro.ProApplication;
import com.qidian.mes.pro.mapper.ProWorkorderMapper;
import com.qidian.mes.pro.service.ProWorkorderService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Calendar;
import java.util.List;

/**
 * @Author 郭靖旖
 * @Company
 * @Time 2023/10/26 14:55
 * @Description
 **/
@Service
public class ProWorkorderServiceImpl implements ProWorkorderService {
    @Autowired
    ProWorkorderMapper proWorkorderMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    //分页查询
    @Override
    public MESResult list(int page, int limit, ProWorkorder proWorkorder) {
        PageHelper.startPage(page,limit);
        List<ProWorkorder> proWorkorderList= proWorkorderMapper.list(proWorkorder);
        PageInfo<ProWorkorder> pageInfo=new PageInfo<>(proWorkorderList);
        return MESResult.ok(pageInfo);
    }

    @Override
    public MESResult selectByPage(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<ProWorkorder> proWorkorderList=proWorkorderMapper.selectallByPage();
        PageInfo<ProWorkorder> pageInfo=new PageInfo<>(proWorkorderList);
        return MESResult.ok(pageInfo);
    }
//自动生成单号
    @Override
    public MESResult GenerateTheWorkOrderNumber() {
        String key="WORKORDERID";
        StringBuilder id=new StringBuilder();
        id.append("WORKORDER");
        Calendar calendar=Calendar.getInstance();
        id.append(calendar.get(Calendar.YEAR)+""+(calendar.get(Calendar.MONTH)+1)+calendar.get(Calendar.DATE));
       Long idTemp=stringRedisTemplate.opsForValue().increment(key);
       String idStr= idTemp.toString();
        if (idStr.length()<5){
            for (int i=0;i<5-idStr.length();i++){
                 idStr="0"+idStr;
            }
        }
        id.append(idStr);
        return MESResult.ok(id);
    }
      //保存新增的工单
    @Override
    public MESResult save(ProWorkorder proWorkorder) {
        proWorkorder.setSourceCode(proWorkorder.getOrderSource());
        if (proWorkorderMapper.save(proWorkorder)==1){
            return MESResult.ok();
        }
        else{
            return MESResult.fail();
        }

    }
    //更改工单的状态
    @Override
    public MESResult updateStatus(String workorderCode) {
        return MESResult.updateIsOk(proWorkorderMapper.updateStatus(workorderCode));
    }

    @Override
    public MESResult deleteWorkorder(String workorderCode) {
        if (proWorkorderMapper.deleteWorkorder(workorderCode)==1) {
            return MESResult.ok();
        }
        else{
           return MESResult.fail();
        }
    }
}
